{# SPDX-License-Identifier: Apache-2.0 -#}

{% extends "admin/base.html" %}

{% macro render_field(label, field, input_id, placeholder=None, class=None, helper=None) %}
<div class="form-group{% if field.errors %} has-error{% endif %}">
  <label for="{{ input_id }}" class="col-sm-2 control-label">{{ label }}
  </label>

  <div class="col-sm-10">
    {{ field(id=input_id, class=class, placeholder=placeholder)}}

    {% if field.errors %}
    <div class="help-block">
      {% for error in field.errors %}
      <div class="warning">{{ error }}</div>
      {% endfor %}
    </div>
    {% endif %}
    {% if helper %}
    <div>
      {{ helper|safe }}
    </div>
    {% endif %}
  </div>
</div>
{% endmacro %}

{% block title %}{% if banner %}{{ banner.name }}{% else %}Create banner{% endif %}{% endblock %}

{% block breadcrumb %}
  <li class="breadcrumb-item"><a href="{{ request.route_path('admin.banner.list') }}">Banners</a></li>
  {% if banner %}
  <li class="breadcrumb-item active">{{ banner.name }}</li>
  {% else %}
  <li class="breadcrumb-item active">Create banner</li>
  {% endif %}
{% endblock %}

{% block content %}
  <div class="row">
    <div class="col-md-12">
      <div class="card card-primary">
        <div class="card-header">
          {% if banner %}
          <h3 class="card-title">Edit Banner</h3>
          {% else %}
          <h3 class="card-title">Create banner</h3>
          {% endif %}
        </div>
        <div class="card-body">
          <form class="form-horizontal" method="POST" action="{{ request.current_route_path() }}">
            <input name="csrf_token" type="hidden" value="{{ request.session.get_csrf_token() }}">

            {% if form.form_errors %}
              {% for error in form.form_errors %}
              <div class="alert alert-danger"><i class="icon fa fa-ban"></i> {{ error }}</div>
              {% endfor %}
            {% endif %}

            {{ render_field("Name", form.name, "banner-name", class="form-control", placeholder="Name") }}

            {{ render_field("Icon Name", form.fa_icon, "banner-fa-icon", class="form-control", placeholder="Fontawesome class", helper="<a href='https://fontawesome.com/v6/search?p=1&m=free' target='_blank' rel='noopener nofollow'>Icons list</a>") }}
            {{ render_field("Text", form.text, "banner-text", class="form-control", placeholder="Banner text content") }}
            {{ render_field("Button URL", form.link_url, "banner-link-url", class="form-control", placeholder="https://sample.com") }}
            {{ render_field("Button label", form.link_label, "banner-link-label", class="form-control", placeholder="See more") }}

            {{ render_field("Active", form.active, "banner-active", helper="<small>Only active banners are displayed.</small>") }}
            {{ render_field("Dismissable", form.dismissable, "banner-dismissable", helper="<small>Only dismissable banners can be dismissed.</small>") }}
            {{ render_field("End date", form.end, "banner-end", placeholder="YYYY-MM-DD", helper="<small>UTC date to work as a safeguard to hide the banner if past this date.</small>")}}


            <div class="form-group">
              <button type="submit" class="btn btn-danger" title="{{ 'Submitting requires staff privileges' if not request.has_permission(Permissions.AdminBannerWrite) }}" {{ "disabled" if not request.has_permission(Permissions.AdminBannerWrite) }}>Submit</button>

              {% if banner %}
              <a class="btn btn-info" href="{{ request.route_path('admin.banner.preview', banner_id=banner.id) }}" rel="nofollow noopener noreferrer" target="_blank"><i class="icon fa fa-eye"></i> Preview Saved</a>

              <button type="button" class="btn btn-danger float-right" data-toggle="modal" data-target="#deleteBanner" {{ "disabled" if not request.has_permission(Permissions.AdminBannerWrite) }} value=>
                <i class="icon fa fa-trash"></i> Delete banner
              </button>
              {% endif %}
            </div>
          </form>
        </div>
      </div>
    </div>
  </div>

{% if banner %}
<div class="modal fade" id="deleteBanner" tabindex="-1" role="dialog">
  <form method="post" action="{{ request.route_path('admin.banner.delete', banner_id=banner.id) }}">
    <input name="csrf_token" type="hidden" value="{{ request.session.get_csrf_token() }}">
    <div class="modal-dialog" role="document">
      <div class="modal-content">
        <div class="modal-header">
          <h4 class="modal-title" id="exampleModalLabel">Delete banner {{ banner.name }}?</h4>
          <button type="button" class="close" data-dismiss="modal">
            <span>&times;</span>
          </button>
        </div>
        <div class="modal-body">
          <p>
            This will permanently destroy the banner and cannot be undone.
          </p>
          {% if banner.is_live %}
          <p>
            <b>Important:</b> the banner is active and is being currently displayed.
          </p>
          {% endif %}
          <hr>
          <input type="hidden" name="banner" value="{{ banner.name }}">
        </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-secondary" data-dismiss="modal">Cancel</button>
          <button type="submit" class="btn btn-danger">Delete</button>
        </div>
      </div>
    </div>
  </form>
</div>
{% endif %}

{% endblock %}
